
package com.jz.community.moduleshopping.confirmOrder.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.jakewharton.rxbinding2.widget.RxTextView;
import com.jz.community.basecomm.bean.baseGoods.ShopListInfo;
import com.jz.community.basecomm.constant.AppConstants;
import com.jz.community.basecomm.utils.CharacterUtils;
import com.jz.community.basecomm.utils.ConverterUtils;
import com.jz.community.basecomm.utils.Preconditions;
import com.jz.community.basecomm.utils.SHelper;
import com.jz.community.moduleshopping.R;
import com.jz.community.basecomm.bean.baseGoods.ShopInfo;
import com.jz.community.moduleshopping.confirmOrder.bean.BaseOrderCouponInfo;
import com.jz.community.moduleshopping.confirmOrder.bean.OrderCouponInfo;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;


public class ConfirmGoodsListAdapter extends BaseQuickAdapter<ShopListInfo, BaseViewHolder> {
    private ConfirmGoodsListItemAdapter confirmGoodsListItemAdapter;
    private List<OrderCouponInfo.MerchantCouponsBean> merchantCoupons;
    private LinearLayout order_shop_coupon_layout;
    private TextView order_shop_coupon_tv;
    private EditText order_shop_et;
    private RXTextChangeListener rxTextChangeListener;

    public void setRxTextChangeListener(RXTextChangeListener rxTextChangeListener) {
        this.rxTextChangeListener = rxTextChangeListener;
    }

    public ConfirmGoodsListAdapter(int layoutResId, @Nullable List<ShopListInfo> data) {
        super(layoutResId, data);
    }

    public void addOrderMerchantCoupons(List<OrderCouponInfo.MerchantCouponsBean> merchantCoupons) {
        this.merchantCoupons = merchantCoupons;
        notifyDataSetChanged();
    }

    @Override
    protected void convert(BaseViewHolder helper, ShopListInfo item) {
        helper.setText(R.id.tv_shop_name, item.getShopName());
        RecyclerView rv_detail_shop_goods_list_recyclerView = helper.getView(R.id.rv_detail_shop_goods_list_recyclerView);
        order_shop_coupon_layout = helper.getView(R.id.order_shop_coupon_layout);
        order_shop_coupon_tv = helper.getView(R.id.order_shop_coupon_tv);
        order_shop_et = helper.getView(R.id.order_shop_et);
        helper.addOnClickListener(R.id.order_shop_coupon_layout);
        rxTextChangeEvents(helper);
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        rv_detail_shop_goods_list_recyclerView.setHasFixedSize(true);
        rv_detail_shop_goods_list_recyclerView.setFocusable(false);
        rv_detail_shop_goods_list_recyclerView.setLayoutManager(layoutManager);
        confirmGoodsListItemAdapter = new ConfirmGoodsListItemAdapter(R.layout.module_shopping_item_commit_order_item, item.getGoodsList());
        rv_detail_shop_goods_list_recyclerView.setAdapter(confirmGoodsListItemAdapter);
        handleOrderShopCoupon(item, helper);
    }

    /**
     * 店铺优惠券
     *
     * @param item
     * @param helper
     */
    private void handleOrderShopCoupon(ShopListInfo item, BaseViewHolder helper) {

        if (Preconditions.isNullOrEmpty(merchantCoupons)) {
            return;
        }

        if (merchantCoupons.get(helper.getAdapterPosition()).getShopId().equals(item.getShopId())) {
            if (!Preconditions.isNullOrEmpty(merchantCoupons.get(helper.getAdapterPosition()).getCouponList())){
                BaseOrderCouponInfo baseOrderCouponInfo = merchantCoupons.get(helper.getAdapterPosition()).getCouponList().get(0);
                if (!Preconditions.isNullOrEmpty(baseOrderCouponInfo)) {
                    SHelper.vis(order_shop_coupon_layout);
                    order_shop_coupon_tv.setText("-" + AppConstants.MONEY + CharacterUtils.roundByScale(ConverterUtils.toDouble(baseOrderCouponInfo.getSubMoney())) + "(" + baseOrderCouponInfo.getCouponName() + ")");
                } else {
                    SHelper.gone(order_shop_coupon_layout);
                }
            }
        }
    }

    @SuppressLint("CheckResult")
    public void rxTextChangeEvents(BaseViewHolder helper) {
        RxTextView.textChangeEvents(order_shop_et)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(onTextChangeEvent -> rxTextChangeListener.rxTextChangeEvents(onTextChangeEvent.text().toString().trim(), helper.getAdapterPosition()));
    }

    @Override
    public int getItemCount() {
        return super.getItemCount();
    }

    public interface RXTextChangeListener {
        void rxTextChangeEvents(String text, int position);
    }
}

